package com.betty.cscams.controller;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.BusSports;
import com.betty.cscams.entity.BusTerm;
import com.betty.cscams.entity.SysUser;
import com.betty.cscams.service.BusFlowService;
import com.betty.cscams.service.BusSportsService;
import com.betty.cscams.service.BusTermService;
import com.betty.cscams.service.SysFileService;
import com.betty.cscams.util.SecurityUtils;
import com.betty.cscams.vo.BusCommonBean;
import com.betty.cscams.vo.BusFlowBean;
import com.betty.cscams.vo.ScoreQueryBean;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ---------------------------
 *  (BusFlowController)
 * ---------------------------
 * 时间：  2022-03-12 17:12:38
 * ---------------------------
 */
@RestController
@RequestMapping("busFlow")
public class BusFlowController {

	@Autowired
	private BusFlowService busFlowService;
	@Autowired
	private SysUserMapper sysUserMapper;

	/**
	 * 流程审核
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/audit")
	public HttpResult auditFlow(@RequestBody BusFlowBean params){
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		List<BusCommonBean> busis = params.getList();
		for (BusCommonBean entity : busis) {
			if("collegeAdmin".equals(sysUser.getType())){
				if(entity.getStatus() != SysConstants.REVIEW_STATUS_5){
					return HttpResult.error("请选择正确的数据进行提交");
				}
			}else if("classAdmin".equals(sysUser.getType())){
				if(entity.getStatus() != SysConstants.REVIEW_STATUS_4 && entity.getStatus() != SysConstants.REVIEW_STATUS_3 ){
					return HttpResult.error("请选择正确的数据进行提交");
				}
			}else if("student".equals(sysUser.getType()) || "teadher".equals(sysUser.getType())){
				if(entity.getStatus() != SysConstants.REVIEW_STATUS_1 && entity.getStatus() != SysConstants.REVIEW_STATUS_2 ){
					return HttpResult.error("请选择正确的数据进行提交");
				}
			}
		}
		return HttpResult.ok(busFlowService.auditFlow(params));

	}

	/**
	 * 流程审核
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/back")
	public HttpResult backFlow(@RequestBody BusFlowBean params){
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		List<BusCommonBean> busis = params.getList();
		for (BusCommonBean entity : busis) {
			if("collegeAdmin".equals(sysUser.getType())){
				if(entity.getStatus() != SysConstants.REVIEW_STATUS_5){
					return HttpResult.error("请选择正确的数据进行提交");
				}
			}else if("classAdmin".equals(sysUser.getType())){
				if(entity.getStatus() != SysConstants.REVIEW_STATUS_4 && entity.getStatus() != SysConstants.REVIEW_STATUS_3 ){
					return HttpResult.error("请选择正确的数据进行提交");
				}
			}else if("student".equals(sysUser.getType()) || "teadher".equals(sysUser.getType())){
					return HttpResult.error("请选择正确的数据进行提交");
			}else if("admin".equals(sysUser.getType())){
				if(entity.getStatus() != SysConstants.REVIEW_STATUS_5 && entity.getStatus() != SysConstants.REVIEW_STATUS_4 && entity.getStatus() != SysConstants.REVIEW_STATUS_3 ){
					return HttpResult.error("请选择正确的数据进行提交");
				}
			}
		}
		return HttpResult.ok(busFlowService.backFlow(params));

	}

	/**
	 * 审核记录查询
	 * @param busiId
	 * @param type
	 * @return
	 */
	@GetMapping(value="/findList")
	public HttpResult findList(Long busiId,Integer type) {
		return HttpResult.ok(busFlowService.findList(busiId,type));
	}
}
